﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Models;
using MySql.Data.MySqlClient;
using Common;

namespace DAL
{
    public class MxExpressionDAL
    {
        //获取所有公式
        public DataTable GetMxExpressionList(Dictionary<string,string> dic) {
            string sql = string.Empty;
            if (!string.IsNullOrWhiteSpace(dic["mxtype"])) {
                sql += " and mxtype like '%" + dic["mxtype"] + "%' ";
            }
            return SqlHelper.ExecuteDataTable("select * from mxexpression " + sql + " where isdel =0");
        }
        //转换成Model
        public MxTypeExpression ToMxExpression(DataRow dr) {
            MxTypeExpression mx = new MxTypeExpression();
            mx.Mid = Convert.ToInt32(dr["Mid"]);
            mx.Mxtype = dr["mxtype"].ToString();
            mx.Kuan = dr["Kuan"].ToString();
            mx.Gao = dr["Gao"].ToString();
            mx.Wlyl = dr["Wlsl"].ToString();
            mx.nozhwidth = dr["nozhwidth"].ToString();
            mx.nozhheight = dr["nozhheight"].ToString();
            mx.nozhxbyl = dr["nozhxbyl"].ToString();
            mx.defaultwidth = dr["defaultwidth"].ToString();
            mx.defaulttopheight = dr["defaulttopheight"].ToString();
            mx.defaultdownheight = dr["defaultdownheight"].ToString();
            mx.defaultxbyl = dr["defaultxbyl"].ToString();
            mx.definewidth = dr["definewidth"].ToString();
            mx.definetopheight = dr["definetopheight"].ToString();
            mx.definedownheight = dr["definedownheight"].ToString();
            mx.definexbyl = dr["definexbyl"].ToString();
            mx.ispb = Convert.ToInt32(dr["ispb"]);
            mx.adduser = dr["adduser"].ToString();
            mx.edituser = dr["edituser"].ToString();
            mx.addtime = dr["addtime"].ToString();
            mx.edittime = dr["edittime"].ToString();
            mx.remarks = dr["remarks"].ToString();
            mx.isdel = Convert.ToInt32(dr["isdel"]);
            return mx;
        }
        //根据主键查询
        public MxTypeExpression GetMxTypeExpressionById(string mid) {
            DataTable dt = SqlHelper.ExecuteDataTable("select * from mxexpression where mid = @mid and isdel =0",new MySqlParameter("@mid",mid));
            if (dt.Rows.Count >0)
            {
                return ToMxExpression(dt.Rows[0]);
            }
            else {
                return null;
            }
        }
        //新增
        public int SaveData(MxTypeExpression mx) {
            int result = -1;
            result = SqlHelper.ExecuteNonQuery(@"insert into mxexpression(mxtype,remarks,kuan,gao,wlsl,nozhwidth,nozhheight,nozhxbyl,defaultwidth,defaulttopheight,defaultdownheight,defaultxbyl,
                                                definewidth,definetopheight,definedownheight,definexbyl,adduser,edituser,addtime,edittime,isdel,ispb) values(@mxtype,@remarks,@kuan,@gao,@wlsl,@nozhwidth,@nozhheight,@nozhxbyl,@defaultwidth,@defaulttopheight,@defaultdownheight,@defaultxbyl,
                                                @definewidth,@definetopheight,@definedownheight,@definexbyl,@adduser,edituser,@addtime,@edittime,@isdel,@ispb)", new MySqlParameter("@mxtype", mx.Mxtype), new MySqlParameter("@remarks", mx.remarks), new MySqlParameter("@kuan", mx.Kuan), new MySqlParameter("@gao", mx.Gao),
                                                new MySqlParameter("@wlsl", mx.Wlyl), new MySqlParameter("@nozhwidth", mx.nozhwidth), new MySqlParameter("@nozhheight", mx.nozhheight), new MySqlParameter("@nozhxbyl", mx.nozhxbyl),new MySqlParameter("@defaultwidth",mx.defaultwidth),
                                                new MySqlParameter("@defaulttopheight", mx.defaulttopheight),new MySqlParameter("@defaultdownheight", mx.defaultdownheight),new MySqlParameter("@defaultxbyl", mx.defaultxbyl),new MySqlParameter("@definewidth", mx.definewidth),
                                                new MySqlParameter("@definetopheight", mx.definetopheight), new MySqlParameter("@definedownheight", mx.definedownheight), new MySqlParameter("@definexbyl", mx.definexbyl), new MySqlParameter("@adduser", RemeberName.Username),
                                                new MySqlParameter("@edituser", RemeberName.Username), new MySqlParameter("@addtime", DateTime.Now.ToString()), new MySqlParameter("@edittime", DateTime.Now.ToString()), new MySqlParameter("@isdel", "0"), new MySqlParameter("@ispb", mx.ispb));
            return result;
        }
        //更新
        public int UpdateData(MxTypeExpression mx) {
            int result = -1;
            result = SqlHelper.ExecuteNonQuery(@"update mxexpression set mxtype=@mxtype,remarks=@remarks,kuan=@kuan,gao=@gao,wlsl=@wlsl,nozhwidth=@nozhwidth,nozhheight=@nozhheight,nozhxbyl=@nozhxbyl,defaultwidth=@defaultwidth,defaulttopheight=@defaulttopheight,defaultdownheight=@defaultdownheight,defaultxbyl=@defaultxbyl,
                                               definewidth=@definewidth,definetopheight=@definetopheight,definedownheight=@definedownheight,definexbyl=@definexbyl,edituser=@edituser,edittime=@edittime,ispb=@ispb where mid=@mid and isdel=0", new MySqlParameter("@mxtype", mx.Mxtype), new MySqlParameter("@remarks", mx.remarks), new MySqlParameter("@kuan", mx.Kuan), new MySqlParameter("@gao", mx.Gao),
                                                new MySqlParameter("@wlsl", mx.Wlyl), new MySqlParameter("@nozhwidth", mx.nozhwidth), new MySqlParameter("@nozhheight", mx.nozhheight), new MySqlParameter("@nozhxbyl", mx.nozhxbyl), new MySqlParameter("@defaultwidth", mx.defaultwidth),
                                                new MySqlParameter("@defaulttopheight", mx.defaulttopheight), new MySqlParameter("@defaultdownheight", mx.defaultdownheight), new MySqlParameter("@defaultxbyl", mx.defaultxbyl), new MySqlParameter("@definewidth", mx.definewidth),
                                                new MySqlParameter("@definetopheight", mx.definetopheight), new MySqlParameter("@definedownheight", mx.definedownheight), new MySqlParameter("@definexbyl", mx.definexbyl),
                                                new MySqlParameter("@edituser", RemeberName.Username), new MySqlParameter("@edittime", DateTime.Now.ToString()), new MySqlParameter("@ispb", mx.ispb), new MySqlParameter("@mid", mx.Mid));
            return result;
        }
        //删除
        public int DelById(string mid) {
            int result = -1;
            result = SqlHelper.ExecuteNonQuery("update mxexpression set isdel =@isdel where mid =@mid ", new MySqlParameter("@mid", mid));
            return result;
        }
        //根据门型查询公式
        public DataTable GetMxExpressionByMxType(string mxtype) {
            return SqlHelper.ExecuteDataTable("select * from mxexpression where mxtype = @mxtype and isdel =0", new MySqlParameter("@mxtype", mxtype));
        }
    }
}
